package com.lzb.play.fragment;

import android.Manifest;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UtilsTransActivity;
import com.lzb.play.BaseNoModelFragment;
import com.lzb.play.Content;
import com.lzb.play.R;
import com.lzb.play.basic.utils.FileUtil;
import com.lzb.play.databinding.FragmentGestureSlideBinding;
import com.lzb.play.dialog.PermissionExplainDialog;
import com.lzb.play.dialog.RecordSaveDialog;
import com.lzb.play.utils.MyMediaRecorder;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link GestureSlideFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GestureSlideFragment extends BaseNoModelFragment<FragmentGestureSlideBinding> implements View.OnClickListener {

    private int type;
    private boolean isRecord;
    private MyMediaRecorder mRecorder;

    @Override
    protected int onCreate() {
        return R.layout.fragment_gesture_slide;
    }

    @Override
    protected void initView() {
        mDataBinding.llMusicType1.setOnClickListener(this);
        mDataBinding.llMusicType2.setOnClickListener(this);
        mDataBinding.llMusicType3.setOnClickListener(this);
        mDataBinding.ivRecord.setOnClickListener(this);

        mRecorder = new MyMediaRecorder();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llMusicType1) {//自然鸣叫
            clearSelection();
            mDataBinding.llMusicType1.setBackgroundResource(R.drawable.music_type_sel);
            mDataBinding.ivMusicType1.setImageResource(R.drawable.icon_ziran_on);
            type = 0;
            int[] rawId = new int[]{R.raw.jiayan, R.raw.maque, R.raw.cuiniao};
            mDataBinding.slidingView.setAudioResources(rawId);
        } else if (id == R.id.llMusicType2) {//欢快跳跃
            clearSelection();
            mDataBinding.llMusicType2.setBackgroundResource(R.drawable.music_type_sel);
            mDataBinding.ivMusicType2.setImageResource(R.drawable.icon_huankuai_on);
            type = 1;
            int[] rawId = new int[]{R.raw.xique, R.raw.huangli, R.raw.bailingniao};
            mDataBinding.slidingView.setAudioResources(rawId);
        } else if (id == R.id.llMusicType3) {//悠长婉转
            clearSelection();
            mDataBinding.llMusicType3.setBackgroundResource(R.drawable.music_type_sel);
            mDataBinding.ivMusicType3.setImageResource(R.drawable.icon_youchang_on);
            type = 2;
            int[] rawId = new int[]{R.raw.huamei, R.raw.laoying, R.raw.shanbanjiu};
            mDataBinding.slidingView.setAudioResources(rawId);
        } else if (id == R.id.ivRecord) {//录音
            if (!isRecord) {
                getPermission();
            } else {
                isRecord = false;
                mRecorder.stopRecording();
                showRecordSaveDialog();
            }
            mDataBinding.ivRecord.setImageResource(isRecord ? R.drawable.icon_record_select : R.drawable.icon_record_no);
        }
    }

    @Override
    protected void initData() {

    }

    private void getPermission() {
        PermissionExplainDialog dialog = new PermissionExplainDialog(mContext);
        dialog.setContent(getString(R.string.get_record_permission));
        PermissionUtils.permission(Manifest.permission.RECORD_AUDIO).explain(new PermissionUtils.OnExplainListener() {
            @Override
            public void explain(@NonNull UtilsTransActivity activity, @NonNull List<String> denied, @NonNull ShouldRequest shouldRequest) {
                dialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        shouldRequest.start(true);
                    }
                }, 500);
            }
        }).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {
                dialog.dismiss();
                startRecord();
            }

            @Override
            public void onDenied() {
                dialog.dismiss();
            }
        }).request();
    }

    //开始录音
    private void startRecord() {
        String musicPath = FileUtil.generateFilePath(Content.Temp, ".mp3");
        try {
            isRecord = true;
            mRecorder.setMyRecAudioFile(FileUtils.getFileByPath(musicPath));
            if (!mRecorder.startRecorder()) {
                isRecord = false;
                ToastUtils.showShort(R.string.audio_fail_tips);
            }
        } catch (Exception e) {
            isRecord = false;
            ToastUtils.showShort(R.string.audio_fail_tips2);
            e.printStackTrace();
        }
        mDataBinding.ivRecord.setImageResource(isRecord ? R.drawable.icon_record_select : R.drawable.icon_record_no);
    }

    private void showRecordSaveDialog() {
        RecordSaveDialog dialog = new RecordSaveDialog(mContext);
        dialog.setAudioPath(mRecorder.getMyRecAudioFile().getPath());
        dialog.show();
    }

    private void clearSelection() {
        mDataBinding.llMusicType1.setBackgroundResource(R.drawable.music_type_bg);
        mDataBinding.ivMusicType1.setImageResource(R.drawable.icon_ziran_off);
        mDataBinding.llMusicType2.setBackgroundResource(R.drawable.music_type_bg);
        mDataBinding.ivMusicType2.setImageResource(R.drawable.icon_huankuai_off);
        mDataBinding.llMusicType3.setBackgroundResource(R.drawable.music_type_bg);
        mDataBinding.ivMusicType3.setImageResource(R.drawable.icon_youchang_off);
    }
}